<?php
/**
 * Created by PhpStorm.
 * User: lawrence
 * Date: 08.06.2014
 * Time: 10:58
 */

class AjaxController extends CI_Controller
{
    function room_status()
    {
        $this->load->database();

        $atenaTotal = $this->db->query("select count(*) as count from roomtable where location='1000' LIMIT 1");
        $romaTotal = $this->db->query("select count(*) as count from roomtable where location='1001' LIMIT 1");
        $venetiaTotal = $this->db->query("select count(*) as count from roomtable where location='1002' LIMIT 1");

        $date = date('Y/m/d');

        $atenaNow = $this->db->query("select count(*) as count from rezerved where  startdate<='" . $date . "' AND enddate>='" . $date . "' AND roomid IN (select id from roomtable where location=1000);");
        $romaNow = $this->db->query("select count(*) as count from rezerved where  startdate<='" . $date . "' AND enddate>='" . $date . "' AND roomid IN (select id from roomtable where location=1001);");
        $venetiaNow = $this->db->query("select count(*) as count from rezerved where  startdate<='" . $date . "' AND enddate>='" . $date . "' AND roomid IN (select id from roomtable where location=1002);");

        $count = $atenaTotal->result_array();
        $altCount = $atenaNow->result_array();
        $atenaFinal = $count[0]['count'] - $altCount[0]['count'];

        $count = $romaTotal->result_array();
        $altCount = $romaNow->result_array();
        $romaFinal =$count[0]['count'] - $altCount[0]['count'];

        $count = $venetiaTotal->result_array();
        $altCount = $venetiaNow->result_array();
        $venetiaFinal = $count[0]['count'] - $altCount[0]['count'];

        echo 'Free rooms in Atena: ' . $atenaFinal . '<br/>';
        echo 'Free rooms in Roma: ' . $romaFinal . '<br/>';
        echo 'Free rooms in Venetia: ' . $venetiaFinal . '<br/>';

        die();
    }

    function check_user()
    {
        $username = $_POST['target'];

        $this->load->database();

        $query = $this->db->query('select * from users where username=\'' . $username . '\' LIMIT 1;');

        if($query->num_rows() > 0)
        {
            echo 'true';
        }
        else
        {
            echo 'false';
        }
    }

    function check_email()
    {
        $email = $_POST['target'];

        $this->load->database();

        $query = $this->db->query('select * from users where email=\'' . $email . '\' LIMIT 1;');

        if($query->num_rows() > 0)
        {
            echo 'true';
        }
        else
        {
            echo 'false';
        }
    }

    function loginUser()
    {
        $username = $_POST['user'];
        $password = $_POST['pass'];

        $this->load->model('db_model_user');

        if($this->db_model_user->login($username, $password))
        {
            echo 'true';

            die();
        }

        echo 'false';
    }

    function logout()
    {
        $this->load->library('session');

        $this->session->set_userdata(array('logged' => false,
                                           'username' => '',
                                           'emai' => ''));

        $data['window'] = 'home';
        $data['pagetitle'] = 'Home';
        $this->load->view('header', $data);

        $this->load->view('home', $data);

        $this->load->view('footer');
    }
} 